//
//  PushMoreViewController.m
//  Movie
//
//  Created by cjy on 16/3/30.
//  Copyright © 2016年 HY. All rights reserved.
//

#import "PushMoreViewController.h"
#import "NetworkHandeler.h"
#import "PushMoreTableViewCell.h"
#import "MovieModel.h"
#import "NetworkingHandle.h"
#import "IntroductionViewController.h"

@interface PushMoreViewController ()<UITableViewDataSource,UITableViewDelegate,NetworkHandelerDelegate>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *arr;
@property (nonatomic,strong)UISegmentedControl *seg;
@property (nonatomic,strong)NSMutableArray *arr2;
@property (nonatomic,strong)MovieModel *model;

@property (nonatomic,strong)MovieModel *model2;

@end

@implementation PushMoreViewController



- (void)viewWillAppear:(BOOL)animated{


    self.tabBarController.tabBar.hidden = YES;
    

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    _seg = [[UISegmentedControl alloc]initWithItems:@[@"按时间排序",@"分享排行榜"]];
    _seg.frame = CGRectMake(0, 0, 100 , 30);
    
    self.navigationItem.titleView = _seg;
    
    [_seg addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    _seg.selectedSegmentIndex = 0;
    
    
    
    [self createtableView];
    
}
- (void)createtableView{

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height + 46) style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
  
    [self.tableView registerClass:[PushMoreTableViewCell class] forCellReuseIdentifier:@"pool"];
    

    [self.tableView registerClass:[PushMoreTableViewCell class] forCellReuseIdentifier:@"pool2"];

    [self handle];
    [self handle2];
    
}

- (void)handle{
    
    
    
        NSString *str =[NSString stringWithFormat:@"http://baobab.wandoujia.com/api/v3/videos?_s=e47f81fc93cb1d1acc1e18293da64efd&categoryId=%ld&f=iphone&net=wifi&num=20&start=0&strategy=date&u=83e89c9ba853b22d017cf56713c230da417b7509&v=1.12.1&vc=403",self.ida];
        [NetworkHandeler handelerJSONWithURL:str delegate:self];
        

    


}

- (void)handelerDidComplete:(id)result{
    
    self.arr = [NSMutableArray array];
    
    NSArray *arritemList = [result objectForKey:@"itemList"];
    for (NSDictionary *dic in arritemList) {
        
        NSDictionary *dicdata = [dic objectForKey:@"data"];
        NSDictionary *dicprovider = [dicdata objectForKey:@"cover"];
        
        _model = [[MovieModel alloc]init];
        _model.coverForDetail = [dicprovider objectForKey:@"feed"];
        _model.category = [dicdata objectForKey:@"category"];
        _model.duration = [[dicdata objectForKey:@"duration" ] floatValue];

        _model.title = [dicdata objectForKey:@"title"];
        _model.descrip = [dicdata objectForKey:@"description"];
        _model.playUrl  = [dicdata objectForKey:@"playUrl"];
        [self.arr addObject:_model];
        
        
    }

    
    [self.tableView reloadData];
}


- (void)handle2{

    [NetworkingHandle networkingWithURL:[NSString stringWithFormat:@"http://baobab.wandoujia.com/api/v3/videos?_s=cca505917809e3f265c24e70306819dc&categoryId=%ld&f=iphone&net=wifi&num=20&start=0&strategy=shareCount&u=83e89c9ba853b22d017cf56713c230da417b7509&v=1.12.1&vc=403",self.ida] completeHandel:^(NSURLResponse *response, NSData *data, id result) {
       
        self.arr2 = [NSMutableArray array];
        
        NSArray *arritemList2 = [result objectForKey:@"itemList"];
        for (NSDictionary *dic2 in arritemList2) {
            
            NSDictionary *dicdata2 = [dic2 objectForKey:@"data"];
            NSDictionary *dicprovider2 = [dicdata2 objectForKey:@"cover"];
            
            _model2 = [[MovieModel alloc]init];
            _model2.title = [dicdata2 objectForKey:@"title"];
            _model2.category = [dicdata2 objectForKey:@"category"];
            _model2.duration= [[dicdata2 objectForKey:@"duration" ] floatValue];
            _model2.coverForDetail = [dicprovider2 objectForKey:@"feed"];

            _model2.descrip = [dicdata2 objectForKey:@"description"];
            _model2.playUrl = [dicdata2 objectForKey:@"playUrl"];
            [self.arr2 addObject:_model2];
            
            
        }
        
        
        
        [self.tableView reloadData];
        
        
    }];



}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

 
    return self.arr.count;



}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    if (_seg.selectedSegmentIndex == 0) {
        
        PushMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
        
        return cell;
        
    }else{
        PushMoreTableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:@"pool2"];
        
               return cell2;
        

        
    
    }

}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    
    NSArray<PushMoreTableViewCell *> *array = [self.tableView visibleCells];
    
    [array enumerateObjectsUsingBlock:^(PushMoreTableViewCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        [obj cellOffset];
    }];
    
    
    
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(PushMoreTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_seg.selectedSegmentIndex == 0) {
        MovieModel *model = self.arr[indexPath.row];
        [cell passModel:model];

    }else{
        MovieModel *model2 = self.arr2[indexPath.row];
        [cell passModel:model2];

        
    }
    
    
           CATransform3D rotation;//3D旋转
        
        rotation = CATransform3DMakeTranslation(0 ,50 ,20);
        
        //逆时针旋转
        
        rotation = CATransform3DScale(rotation, 0.9, .9, 1);
        
        rotation.m34 = 1.0/ -600;
        
        cell.layer.shadowColor = [[UIColor blackColor]CGColor];
        cell.layer.shadowOffset = CGSizeMake(10, 10);
        cell.alpha = 0;
        
        cell.layer.transform = rotation;
        
        [UIView beginAnimations:@"rotation" context:NULL];
        //旋转时间
        [UIView setAnimationDuration:0.6];
        cell.layer.transform = CATransform3DIdentity;
        cell.alpha = 1;
        cell.layer.shadowOffset = CGSizeMake(0, 0);
        [UIView commitAnimations];
    
    [cell cellOffset];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 240;
    
}


- (void)segmentAction:(UISegmentedControl *)seg{
    
    switch (seg.selectedSegmentIndex) {
        case 0:
            
            [self.tableView reloadData];
            break;
            
        case 1:
            
           
            [self.tableView reloadData];
            
            break;
        default:
            break;
    }
    
    
    
    
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    IntroductionViewController *introduction = [[IntroductionViewController alloc]init];
    
    if (_seg.selectedSegmentIndex == 0) {
        _model = self.arr [indexPath.row];
        introduction.icon = _model.coverForDetail;
        introduction.titlea = _model.title;
        introduction.little = _model.category;
        introduction.duration = _model.duration;
        introduction.des = _model.descrip;
        introduction.playUrl = _model.playUrl;
        introduction.model = _model;
        
    }else{
    
       _model2 = self.arr2 [indexPath.row];
        introduction.icon = _model2.coverForDetail;
        introduction.titlea = _model2.title;
        introduction.little = _model2.category;
        introduction.duration = _model2.duration;
        introduction.des = _model2.descrip;
        introduction.playUrl = _model2.playUrl;
        introduction.model = _model2;
    }
      [self.navigationController pushViewController:introduction animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
